package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcLabReportEntity;
import com.dhcc.cdc.busi.service.ICdcLabReportService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 检验报告
 * 
 * @ClassName: LabReportController
 * @Description: TODO
 * @author jumper
 * @date 2021-04-26 16:21:41
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/labreport")
public class CdcLabReportController extends BaseController {

	private String PREFIX = "/backend/cdc/labreport/";

	@Autowired
	private ICdcLabReportService labReportService;
	

    /**
     * 跳转到检验报告列表首页
     */
    @RequestMapping("")
    @Permission("labreport:list")
    public String index() {
        return PREFIX + "labreport.html";
    }
	
    /**
     * 跳转到新增检验报告
     */
    @RequestMapping("/to_add")
    @Permission("labreport:add")
    public String toAdd() {
        return PREFIX + "labreport_add.html";
    }
	
	/**
     * 跳转到修改检验报告
     */
    @RequestMapping("/to_update/{labReportId}")
    @Permission("labreport:update")
    public String toUpdate(@PathVariable Long labReportId, Model model) {
        CdcLabReportEntity labReport = this.labReportService.selectById(labReportId);
        model.addAttribute("labReport",labReport);
        return PREFIX + "labreport_edit.html";
    }

    /**
     * 获取检验报告列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("labreport:list")
    public Object list(@RequestParam Map<String, Object> params) {
        List<CdcLabReportEntity> result = null;
        Page<CdcLabReportEntity> page = new PageFactory<CdcLabReportEntity>().defaultPage();
        if((CommonUtil.isNotEmpty(params.get("NotQuery")))&&(params.get("NotQuery").toString().equals("true"))){
            result = new ArrayList<CdcLabReportEntity>();
        }else{
            result = this.labReportService.page(page,params);
        }

        page.setRecords((List<CdcLabReportEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增检验报告
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("labreport:add")
    @BussinessLog(value = "新增检验报告")
    public Object add(CdcLabReportEntity labReport) {
        if (CommonUtil.isOneEmpty(labReport)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        labReport.insert();
        return R.ok();
    }

    /**
     * 删除检验报告
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("labreport:delete")
    @BussinessLog(value = "删除检验报告")
    public Object delete(@RequestParam Long labReportId) {
	
        this.labReportService.deleteById(labReportId);

        return R.ok();
    }

    /**
     * 修改检验报告
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("labreport:update")
    @BussinessLog(value = "修改检验报告")
    public Object update(CdcLabReportEntity labReport) {
        if (CommonUtil.isOneEmpty(labReport)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcLabReportEntity old = this.labReportService.selectById(labReport.getId());

        old.setEpisodeId(labReport.getEpisodeId());
        old.setXcode(labReport.getXcode());
        old.setLabVisitId(labReport.getLabVisitId());
        old.setReportId(labReport.getReportId());
        old.setReportNum(labReport.getReportNum());
        old.setResult(labReport.getResult());
        old.setBusLabReportStatusId(labReport.getBusLabReportStatusId());
        old.setReportDate(labReport.getReportDate());
        old.setReportUserName(labReport.getReportUserName());
        old.setCheckDate(labReport.getCheckDate());
        old.setCheckUserName(labReport.getCheckUserName());
        old.setDataId(labReport.getDataId());
        old.setIsActive(labReport.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
